//
//  FFRedPacketsAllDetailRecordVC.m
//  LiMaoRedPackets
//
//  Created by JayLuo on 2020/8/28.
//

#import "FFRedPacketsAllDetailRecordVC.h"
#import "Masonry.h"
#import "ReactiveObjC.h"
#import "FFRedPacketsRecordCell.h"
#import "FFRedPacketsRecordModel.h"
#import "MJExtension.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define NUMBERS_OF_YEAES 5

@interface FFRedPacketsAllDetailRecordVC()<UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) UIPickerView *pickerView;
@property(nonatomic,strong) UIView *cotainerView;
@property(nonatomic,weak) UIButton *receivedButton;
@property(nonatomic,weak) UIButton *sendButton;
@property(nonatomic,weak) UIButton *switchYearsButton;
@property(nonatomic,weak) UIView *datePickerView;
@property(nonatomic,weak) UILabel *userNameLabel;
@property(nonatomic,weak) UIImageView *avatarImageView;
@property(nonatomic,weak) UILabel *moneyLabel;
@property(nonatomic,weak) UILabel *leftLabel1;
@property(nonatomic,weak) UILabel *leftLabel;
@property(nonatomic,weak) UILabel *rightLabel;
@property(nonatomic,strong) NSMutableArray *pickerViewArray;
@property(nonatomic,copy) NSString *selectedYear;
@property(nonatomic, assign) int selectedIndex;
@property(nonatomic,strong) NSArray *years;
@property(nonatomic,strong) NSString *currentYear;
@property(nonatomic,strong) NSMutableArray <FFRedPacketsRecord *>*recordInAndOutArray;
@end

@implementation FFRedPacketsAllDetailRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupHeader];
    [self setupBody];
    self.selectedIndex = 0;
    self.selectedYear = self.currentYear;
    [self getYears];
    [self getStatisticsInAndOutYear:self.currentYear];
    [self getRecordInInYear:self.currentYear];
}

#pragma mark - UI

- (void)setupHeader {
    self.title = LLang(@"红包记录");
    self.navigationBar.showBackButton = YES;
    __weak typeof(self) weakSelf = self;
    self.navigationBar.onBack = ^{
        [weakSelf close];
    };
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.navigationBar.backgroundColor = [UIColor colorWithRed:209/255.0 green:68/255.0 blue:68/255.0 alpha:1.0];
    });
    
    self.navigationBar.style = WKNavigationBarStyleWhite;
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navigationBar.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    UIButton * switchYearsButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [switchYearsButton setTitle:LLang(@"--年") forState:(UIControlStateNormal)];
    [switchYearsButton.titleLabel setFont:[UIFont fontWithName:@"PingFangSC-Regular" size:14]];
    _switchYearsButton = switchYearsButton;
    @weakify(self);
    [[switchYearsButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.datePickerView removeFromSuperview];
        UIView *datePickerView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 200)];
        datePickerView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:datePickerView];
        self.datePickerView = datePickerView;
        [UIView animateWithDuration:.2 animations:^{
            datePickerView.frame = CGRectMake(0, SCREEN_HEIGHT - 200, SCREEN_WIDTH, 200);
        } completion:^(BOOL finished) {
            
        }];
        [datePickerView addSubview:self.pickerView];
        [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(datePickerView);
            make.height.equalTo(@200);
        }];
        //  设置左上角取消按钮
        UIButton *cancleBtn = [UIButton new];
        [cancleBtn setTitle:LLang(@"取消") forState:UIControlStateNormal];
        [cancleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        cancleBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [cancleBtn addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
        [datePickerView addSubview:cancleBtn];
        [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(15);
            make.top.equalTo(self.pickerView.mas_top);
        }];
        
        //  设置右上角确定按钮
        UIButton *confirmBtn = [UIButton new];
        [confirmBtn setTitle:LLang(@"确定") forState:UIControlStateNormal];
        [confirmBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
        [datePickerView addSubview:confirmBtn];
        [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.view).offset(-15);
            make.top.equalTo(self.pickerView.mas_top);
        }];
        
        
    }];
    [self.view addSubview:switchYearsButton];
     CGFloat statusHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    [switchYearsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navigationBar.mas_top).offset(statusHeight + 6);
        make.right.equalTo(self.view.mas_right).offset(-15);
    }];
    
}

- (void)setupBody {
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, - 1000, self.view.frame.size.width, 1000)];
    bgView.layer.backgroundColor = [UIColor colorWithRed:209/255.0 green:68/255.0 blue:68/255.0 alpha:1.0].CGColor;
    [self.tableView addSubview:bgView];
    
    UIView *headView = [[UIView alloc] init];
    headView.frame = CGRectMake(0,0,SCREEN_WIDTH,300);
    headView.backgroundColor = [UIColor colorWithRed:209/255.0 green:68/255.0 blue:68/255.0 alpha:1.0];
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0,12,SCREEN_WIDTH,30);
    view.layer.backgroundColor = [UIColor clearColor].CGColor;
    [headView addSubview:view];
    
    // 分割线
    UIView *vline = [[UIView alloc] init];
    vline.frame = CGRectMake(0,0,0,1);
    vline.layer.backgroundColor = [UIColor clearColor].CGColor;
    [view addSubview:vline];
    [vline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@20);
        make.width.equalTo(@1);
        make.centerX.equalTo(view.mas_centerX);
        make.centerY.equalTo(view.mas_centerY);
    }];
    

    UIButton *receivedButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [receivedButton setTitle:LLang(@"收到的红包") forState:(UIControlStateNormal)];
    [receivedButton setTitleColor:[UIColor colorWithRed:255/255.0 green:251/255.0 blue:165/255.0 alpha:1.0] forState:(UIControlStateSelected)];
    [receivedButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [receivedButton setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.24]] forState:UIControlStateSelected];
    [receivedButton setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:209/255.0 green:68/255.0 blue:68/255.0 alpha:1.0]] forState:UIControlStateNormal];
    [receivedButton.titleLabel setFont:[UIFont fontWithName:@"PingFangSC-Regular" size:14]];
    receivedButton.layer.cornerRadius = 15;
    receivedButton.layer.masksToBounds = YES;
    [receivedButton setSelected:YES];
    @weakify(self);
    [[receivedButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIButton * _Nullable x) {
        @strongify(self);
        if (x.selected == YES) {
            return;
        }
        x.selected = YES;
        self.sendButton.selected = NO;
        self.selectedIndex = 0;
        [self getRecordInInYear:self.selectedYear];
        [self refreshType:self.selectedIndex complete:^(id response) {
            NSDictionary *dict = (NSDictionary *)response;
            if (dict) {
                NSNumber *luck_num = dict[@"luck_num"];
                NSNumber *total_amount = dict[@"total_amount"];
                NSNumber *total_num = dict[@"total_num"];
                self.userNameLabel.text = [NSString stringWithFormat:LLang(@"%@共收到"),  [WKApp shared].loginInfo.extra[@"name"]];
                NSString *avatarString = [WKAvatarUtil getAvatar:[WKApp shared].loginInfo.uid];
                [self.avatarImageView setImageWithURL:[NSURL URLWithString:avatarString] placeholderImage:[WKApp shared].config.defaultAvatar];
                NSMutableAttributedString *moneyLabelString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%.2f", total_amount.doubleValue/100.0] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"AvenirNext-DemiBold" size: 30],NSForegroundColorAttributeName: [UIColor whiteColor]}];

                [moneyLabelString addAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"AvenirNext-DemiBold" size: 16],NSForegroundColorAttributeName: [UIColor whiteColor]} range:NSMakeRange(0, 1)];
                
                self.moneyLabel.attributedText = moneyLabelString;
                
                NSString *number = [NSString stringWithFormat:@"%@",total_num];
                NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:LLang(@"收到红包 %@ 个"), number] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.0]}];
                
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(0, 3)];
//
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.000000]} range:NSMakeRange(4, number.length + 1)];
//
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(4 + number.length + 1, 1)];
                
                self.leftLabel.attributedText = string;
                
                NSString *rightNumber = [NSString stringWithFormat:@"%@",luck_num];
                NSMutableAttributedString *rightString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:LLang(@"手气最佳 %@ 个"), rightNumber] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.0]}];
                
//                [rightString addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(0, 3)];
//
//                [rightString addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.000000]} range:NSMakeRange(4, rightNumber.length + 1)];
//
//                [rightString addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(4 + rightNumber.length + 1, 1)];
                
                self.rightLabel.attributedText = rightString;

            }
        }];
        [self.cotainerView removeFromSuperview];
        self.cotainerView = nil;
        [headView addSubview:self.cotanierView];
        [self.cotanierView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(headView.mas_bottom);
            make.height.equalTo(@52);
            make.left.equalTo(headView).offset(15);
            make.right.equalTo(headView).offset(-15);
        }];
    }];
    [view addSubview:receivedButton];
    self.receivedButton = receivedButton;
    [receivedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(vline.mas_left).offset(-10);
        make.centerY.equalTo(view.mas_centerY);
        make.width.equalTo(@100);
        make.height.equalTo(@30);
    }];
    
    UIButton *sendButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [sendButton setTitle:LLang(@"发出的红包") forState:(UIControlStateNormal)];
    [sendButton setTitleColor:[UIColor colorWithRed:255/255.0 green:251/255.0 blue:165/255.0 alpha:1.0] forState:(UIControlStateSelected)];
    [sendButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [sendButton setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.24]] forState:UIControlStateSelected];
    [sendButton setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:209/255.0 green:68/255.0 blue:68/255.0 alpha:1.0]] forState:UIControlStateNormal];
    [sendButton.titleLabel setFont:[UIFont fontWithName:@"PingFangSC-Regular" size:14]];
    sendButton.layer.cornerRadius = 15;
    sendButton.layer.masksToBounds = YES;
    [[sendButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIButton * _Nullable x) {
        @strongify(self);
        if (x.selected == YES) {
            return;
        }
        x.selected = YES;
        self.selectedIndex = 1;
        self.receivedButton.selected = NO;
        [self getRecordOutInYear:self.selectedYear];
        [self refreshType:self.selectedIndex complete:^(id response) {
            NSDictionary *dict = (NSDictionary *)response;
            if (dict) {
                NSNumber *total_amount = dict[@"total_amount"];
                NSNumber *total_num = dict[@"total_num"];
                self.userNameLabel.text = [NSString stringWithFormat:LLang(@"%@共发出"),  [WKApp shared].loginInfo.extra[@"name"]];
                NSString *avatarString = [WKAvatarUtil getAvatar:[WKApp shared].loginInfo.uid];
                [self.avatarImageView setImageWithURL:[NSURL URLWithString:avatarString] placeholderImage:[WKApp shared].config.defaultAvatar];
                NSMutableAttributedString *moneyLabelString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%.2f", total_amount.doubleValue/100.0] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"AvenirNext-DemiBold" size: 30],NSForegroundColorAttributeName: [UIColor whiteColor]}];
                
                [moneyLabelString addAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"AvenirNext-DemiBold" size: 16],NSForegroundColorAttributeName: [UIColor whiteColor]} range:NSMakeRange(0, 1)];
                
                self.moneyLabel.attributedText = moneyLabelString;
                
                NSString *number = [NSString stringWithFormat:@"%ld",total_num.integerValue];
                NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:LLang(@"发出红包 %@ 个"), number] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.0]}];
                
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(0, 3)];
//
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.000000]} range:NSMakeRange(4, number.length + 1)];
//
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(4 + number.length + 1, 1)];
                
                self.leftLabel1.attributedText = string;
    
            }
        }];
        [self.cotainerView removeFromSuperview];
        self.cotainerView = nil;
        [headView addSubview:self.cotanierView];
        [self.cotanierView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(headView.mas_bottom);
            make.height.equalTo(@52);
            make.left.equalTo(headView).offset(15);
            make.right.equalTo(headView).offset(-15);
        }];
    }];
    [view addSubview:sendButton];
    self.sendButton = sendButton;
    [sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(vline.mas_right).offset(10);
        make.centerY.equalTo(view.mas_centerY);
        make.width.equalTo(@100);
        make.height.equalTo(@30);
    }];
    
    UIImage *avatarImage = [self imageName:@"avatar_male"];
    UIImageView *avatarImageView = [[UIImageView alloc] initWithImage:avatarImage];
    [headView addSubview:avatarImageView];
    avatarImageView.layer.cornerRadius = 40;
    avatarImageView.layer.masksToBounds = YES;
    avatarImageView.layer.borderWidth = 3;
    avatarImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom).mas_offset(30);
        make.centerX.equalTo(headView.mas_centerX);
        make.width.height.equalTo(@80);
    }];
    
    _avatarImageView = avatarImageView;
    
    UILabel *userNameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    userNameLabel.textColor = [UIColor colorWithRed:248/255.0 green:120/255.0 blue:120/255.0 alpha:1.0];
    userNameLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    userNameLabel.text = [NSString stringWithFormat:LLang(@"%@共收到"), @"--"];
    userNameLabel.textAlignment = NSTextAlignmentCenter;
    userNameLabel.numberOfLines = 0;
    [headView addSubview:userNameLabel];
    _userNameLabel = userNameLabel;
    [userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(avatarImageView.mas_bottom).mas_offset(15);
        make.centerX.equalTo(headView.mas_centerX);
    }];
    
    UILabel *moneyLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    NSMutableAttributedString *moneyLabelString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@", @"--"] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"AvenirNext-DemiBold" size: 30],NSForegroundColorAttributeName: [UIColor whiteColor]}];

    [moneyLabelString addAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"AvenirNext-DemiBold" size: 16],NSForegroundColorAttributeName: [UIColor whiteColor]} range:NSMakeRange(0, 1)];
    
    moneyLabel.attributedText = moneyLabelString;
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    moneyLabel.numberOfLines = 0;
    [headView addSubview:moneyLabel];
    _moneyLabel = moneyLabel;
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userNameLabel.mas_bottom).mas_offset(5);
        make.centerX.equalTo(headView.mas_centerX);
    }];

    [headView addSubview:self.cotanierView];
    [self.cotanierView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(headView.mas_bottom);
        make.height.equalTo(@52);
        make.left.equalTo(headView).offset(15);
        make.right.equalTo(headView).offset(-15);
    }];
    
    self.tableView.tableHeaderView = headView;
}

#pragma mark - Lazy
- (UIView *)cotanierView {
    if (_cotainerView == nil) {
        _cotainerView = [[UIView alloc] init];
        _cotainerView.backgroundColor = [UIColor clearColor];
        // 分割线
        UIView *lineView = [[UIView alloc] init];
        lineView.frame = CGRectMake(0,0,0,1);
        lineView.layer.backgroundColor = [UIColor colorWithRed:228/255.0 green:99/255.0 blue:99/255.0 alpha:1.0].CGColor;
        [_cotainerView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_cotainerView.mas_top);
            make.height.equalTo(@1);
            make.left.equalTo(_cotainerView).offset(15);
            make.right.equalTo(_cotainerView).offset(-15);
        }];
        
        if (self.selectedIndex == 0) {
            // 分割线
            UIView *vlineView = [[UIView alloc] init];
            vlineView.frame = CGRectMake(0,0,0,1);
            vlineView.layer.backgroundColor = [UIColor colorWithRed:228/255.0 green:99/255.0 blue:99/255.0 alpha:1.0].CGColor;
            [_cotainerView addSubview:vlineView];
            [vlineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@20);
                make.width.equalTo(@1);
                make.centerX.equalTo(_cotainerView.mas_centerX);
                make.centerY.equalTo(_cotainerView.mas_centerY);
            }];
            
            UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectZero];
            leftLabel.textAlignment = NSTextAlignmentCenter;
            leftLabel.numberOfLines = 2;
            NSString *number = @"--";
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:LLang(@"收到红包 %@ 个"), number] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.0]}];
            
//            [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(0, 3)];
//
//            [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.000000]} range:NSMakeRange(4, number.length + 1)];
//
//            [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(4 + number.length + 1, 1)];
            
            leftLabel.attributedText = string;
            [_cotainerView addSubview:leftLabel];
            
            [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_cotainerView.mas_left);
                make.right.equalTo(vlineView.mas_left);
                make.centerY.equalTo(_cotainerView.mas_centerY);
            }];
            
            _leftLabel = leftLabel;
            
            UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectZero];
            rightLabel.textAlignment = NSTextAlignmentCenter;
            rightLabel.numberOfLines = 2;
            NSString *rightNumber = @"--";
            NSMutableAttributedString *rightString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:LLang(@"手气最佳 %@ 个"), rightNumber] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.0]}];
            
//            [rightString addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(0, 3)];
//
//            [rightString addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.000000]} range:NSMakeRange(4, rightNumber.length + 1)];
//
//            [rightString addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(4 + rightNumber.length + 1, 1)];
            
            rightLabel.attributedText = rightString;
            [_cotainerView addSubview:rightLabel];
            
            [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(vlineView.mas_right);
                make.right.equalTo(_cotainerView.mas_right);
                make.centerY.equalTo(_cotainerView.mas_centerY);
            }];
            
            _rightLabel = rightLabel;
        }else {
            UILabel *leftLabel1 = [[UILabel alloc]initWithFrame:CGRectZero];
            leftLabel1.textAlignment = NSTextAlignmentCenter;
            leftLabel1.numberOfLines = 2;
            NSString *number = @"--";
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:LLang(@"发出红包 %@ 个"), number] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.0]}];
            
//            [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(0, 3)];
//
//            [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.000000]} range:NSMakeRange(4, number.length + 1)];
//
//            [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(4 + number.length + 1, 1)];
            
            leftLabel1.attributedText = string;
            [_cotainerView addSubview:leftLabel1];
            
            [leftLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_cotainerView.mas_left);
                make.right.equalTo(_cotainerView.mas_right);
                make.centerY.equalTo(_cotainerView.mas_centerY);
            }];
            
            _leftLabel1 = leftLabel1;
        }
    }
    return _cotainerView;
}


- (NSString *)currentYear {
    if (!_currentYear) {
        NSDate *senddate = [NSDate date];
        NSDateFormatter  *dateformatter = [[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"yyyy"];
        _currentYear = [dateformatter stringFromDate:senddate];
    }
    return _currentYear;
}

- (NSMutableArray *)recordInAndOutArray {
    if (!_recordInAndOutArray) {
        _recordInAndOutArray = [NSMutableArray array];
    }
    return _recordInAndOutArray;;
}

- (NSMutableArray *)pickerViewArray {
    if (_pickerViewArray == nil) {
        _pickerViewArray = [NSMutableArray array];
    }
    return _pickerViewArray;
}

- (UIPickerView *)pickerView {
    if (_pickerView == nil) {
        self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 400)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        self.selectedYear = self.pickerViewArray.firstObject;
    }
    
    return _pickerView;
}

// 设置列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

// 设置指定列包含的项数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
   
    return self.pickerViewArray.count;
}

// 设置每个选项显示的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [self.pickerViewArray objectAtIndex:row];
}

// 用户进行选择
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.selectedYear = self.pickerViewArray[row];
        [self.pickerView reloadComponent:0];
        [self.pickerView selectRow:row inComponent:0 animated:YES];
    }
}

- (UITableView *)tableView {
    if(!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        UIEdgeInsets separatorInset   = _tableView.separatorInset;
        separatorInset.right          = 0;
        _tableView.separatorInset = separatorInset;
        _tableView.backgroundColor=[UIColor clearColor];
        _tableView.sectionIndexBackgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.estimatedRowHeight = 50;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.decelerationRate = 0.1;
        [_tableView registerClass:FFRedPacketsRecordCell.class forCellReuseIdentifier:[FFRedPacketsRecordCell cellId]];
        
    }
    return _tableView;
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.recordInAndOutArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FFRedPacketsRecordCell *cell =  [tableView dequeueReusableCellWithIdentifier:[FFRedPacketsRecordCell cellId]];
    if (self.selectedIndex == 0) {
        [cell refreshWithModel:self.recordInAndOutArray[indexPath.row] type:(LIMRedPacketsRecordInList)];
    }else {
        [cell refreshWithModel:self.recordInAndOutArray[indexPath.row] type:(LIMRedPacketsRecordOutList)];
    }
    
    return cell;
}

#pragma mark - Actions

- (void)cancle {
   
    [UIView animateWithDuration:.2 animations:^{
        self.datePickerView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 200);
    } completion:^(BOOL finished) {
        [self.datePickerView removeFromSuperview];
    }];
}

- (void)confirm {
    [UIView animateWithDuration:.2 animations:^{
        self.datePickerView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 200);
    } completion:^(BOOL finished) {
        [self.datePickerView removeFromSuperview];
        [self.switchYearsButton setTitle:[NSString stringWithFormat:@"%@年", self.selectedYear] forState:(UIControlStateNormal)];
        [self getStatisticsInAndOutYear:self.selectedYear];
        if (self.selectedIndex == 0) {
            [self getRecordInInYear:self.selectedYear];
        }else {
            [self getRecordOutInYear:self.selectedYear];
        }
        
        
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)close {
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (UIImage*)imageName:(NSString*)name {
    return [[WKApp shared] loadImage:name moduleID:@"LiMaoRedPackets"];
}

- (void)dealloc {
    NSLog(@"%s", __FUNCTION__);
}

#pragma mark - Request
- (void)refreshType:(int)type complete:(void(^)(id response))complete {
    // 网络请求
    __weak typeof(self) weakSelf = self;
    
    [[WKAPIClient sharedClient] GET:[NSString stringWithFormat:@"redpacket/statistics/%@", type == 0 ? @"in":@"out"] parameters:@{@"year":weakSelf.selectedYear}].then(^(NSDictionary *dict){
        if (complete) {
            complete(dict);
        }
    }).catch(^(NSError *error){
        if (complete) {
            complete(nil);
        }
    });
}

- (void)getStatisticsInAndOutYear:(NSString *)year {
    [self refreshType:self.selectedIndex complete:^(id response) {
        NSDictionary *dict = (NSDictionary *)response;
        if (dict) {
            if (self.selectedIndex == 0) {
                NSNumber *luck_num = dict[@"luck_num"];
                NSNumber *total_amount = dict[@"total_amount"];
                NSNumber *total_num = dict[@"total_num"];
                self.userNameLabel.text = [NSString stringWithFormat:LLang(@"%@共收到"),  [WKApp shared].loginInfo.extra[@"name"]];
                NSString *avatarString = [WKAvatarUtil getAvatar:[WKApp shared].loginInfo.uid];
                [self.avatarImageView setImageWithURL:[NSURL URLWithString:avatarString] placeholderImage:[WKApp shared].config.defaultAvatar];
                NSMutableAttributedString *moneyLabelString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%.2f", total_amount.doubleValue/100.0] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"AvenirNext-DemiBold" size: 30],NSForegroundColorAttributeName: [UIColor whiteColor]}];
                
                [moneyLabelString addAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"AvenirNext-DemiBold" size: 16],NSForegroundColorAttributeName: [UIColor whiteColor]} range:NSMakeRange(0, 1)];
                
                self.moneyLabel.attributedText = moneyLabelString;
                
                NSString *number = [NSString stringWithFormat:@"%@",total_num];
                NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:LLang(@"收到红包 %@ 个"), number] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.0]}];
                
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(0, 3)];
//
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.000000]} range:NSMakeRange(4, number.length + 1)];
//
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(4 + number.length + 1, 1)];
                
                self.leftLabel.attributedText = string;
                
                NSString *rightNumber = [NSString stringWithFormat:@"%@",luck_num];
                NSMutableAttributedString *rightString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:LLang(@"手气最佳 %@ 个"), rightNumber] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.0]}];
                
//                [rightString addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(0, 3)];
//
//                [rightString addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.000000]} range:NSMakeRange(4, rightNumber.length + 1)];
//
//                [rightString addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(4 + rightNumber.length + 1, 1)];
                
                self.rightLabel.attributedText = rightString;
            }else {
                
                NSNumber *total_amount = dict[@"total_amount"];
                NSNumber *total_num = dict[@"total_num"];
                self.userNameLabel.text = [NSString stringWithFormat:LLang(@"%@共发出"),  [WKApp shared].loginInfo.extra[@"name"]];
                NSString *avatarString = [WKAvatarUtil getAvatar:[WKApp shared].loginInfo.uid];
                [self.avatarImageView setImageWithURL:[NSURL URLWithString:avatarString] placeholderImage:[WKApp shared].config.defaultAvatar];
                NSMutableAttributedString *moneyLabelString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%.2f", total_amount.doubleValue/100.0] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"AvenirNext-DemiBold" size: 30],NSForegroundColorAttributeName: [UIColor whiteColor]}];
                
                [moneyLabelString addAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"AvenirNext-DemiBold" size: 16],NSForegroundColorAttributeName: [UIColor whiteColor]} range:NSMakeRange(0, 1)];
                
                self.moneyLabel.attributedText = moneyLabelString;
                
                NSString *number = [NSString stringWithFormat:@"%ld",total_num.integerValue];
                NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:LLang(@"发出红包 %@ 个"), number] attributes: @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.0]}];
                
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(0, 3)];
//                
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.000000]} range:NSMakeRange(4, number.length + 1)];
//                
//                [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:121/255.0 blue:121/255.0 alpha:1.000000]} range:NSMakeRange(4 + number.length + 1, 1)];
                
                self.leftLabel1.attributedText = string;
            }
            
        }
    }];
}

- (void)getYears {
    __weak typeof(self) weakSelf = self;
    [[WKAPIClient sharedClient] GET:[NSString stringWithFormat:@"redpacket/years"] parameters:@{}].then(^(NSDictionary *dict){
        weakSelf.pickerViewArray = (NSMutableArray *)dict;
        [weakSelf.switchYearsButton setTitle:[NSString stringWithFormat:LLangW(@"%@年",weakSelf), weakSelf.pickerViewArray.firstObject] forState:(UIControlStateNormal)];
        
    }).catch(^(NSError *error){
         
    });
}

- (void)getRecordInInYear:(NSString *)year {
    __weak typeof(self) weakSelf = self;
    [[WKAPIClient sharedClient] GET:[NSString stringWithFormat:@"redpacket/record/in"] parameters:@{@"year":year, @"page_index":@1, @"page_size":@2000}].then(^(NSDictionary *dict){
        NSArray *tempArray = (NSArray *)dict;
        [weakSelf.recordInAndOutArray removeAllObjects];
        for (NSDictionary *tempDict in tempArray) {
             FFRedPacketsRecord *model = [FFRedPacketsRecord mj_objectWithKeyValues:tempDict];
            [weakSelf.recordInAndOutArray addObject:model];
        }
        [weakSelf.tableView reloadData];
        
    }).catch(^(NSError *error){
         
    });
}

- (void)getRecordOutInYear:(NSString *)year {
    __weak typeof(self) weakSelf = self;
    [[WKAPIClient sharedClient] GET:[NSString stringWithFormat:@"redpacket/record/out"] parameters:@{@"year":year, @"page_index":@1, @"page_size":@2000}].then(^(NSDictionary *dict){
        NSArray *tempArray = (NSArray *)dict;
        [weakSelf.recordInAndOutArray removeAllObjects];
        for (NSDictionary *tempDict in tempArray) {
             FFRedPacketsRecord *model = [FFRedPacketsRecord mj_objectWithKeyValues:tempDict];
            [weakSelf.recordInAndOutArray addObject:model];
        }
        [weakSelf.tableView reloadData];
        
    }).catch(^(NSError *error){
         
    });
}

@end
